﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Yuans.BinaryConfig.BlobIO
{
    public class TAS57xxCfgBlobValueIO : BlobValueIO
    {
        private byte[] mBlobValue;

        public byte[] BlobValue
        {
            get { return mBlobValue; }
            set { mBlobValue = value; }
        }
        private byte[] ParseLine(string stringLine)
        {
            string[] splitStringUnit = stringLine.Split(new string[] { "  ","   "," " },
                                             StringSplitOptions.RemoveEmptyEntries);

            if(splitStringUnit[0].Contains("X"))
            { 

            splitStringUnit[0] = splitStringUnit[0].Substring(1);

            byte[] rawBytes = new byte[splitStringUnit.Length + 1];

            rawBytes[0] = byte.Parse(splitStringUnit[0], System.Globalization.NumberStyles.HexNumber);
            rawBytes[1] = (byte)(splitStringUnit.Length - 1);

            for (int i = 2; i < rawBytes.Length; i++)
            {
                rawBytes[i] = byte.Parse(splitStringUnit[i-1], System.Globalization.NumberStyles.HexNumber);
            }

            return rawBytes;
            }
            else
            {
                return null;
            }
        }
        public void ImportFile(string fileName)
        {
            List<byte> blobList = new List<byte>();
            byte[] lineRawBytes;
            try
            {
                // Create an instance of StreamReader to read from a file. 
                // The using statement also closes the StreamReader. 
                using (StreamReader fileReader = new StreamReader(fileName))
                {
                    string line;
                    // Read and display lines from the file until the end of  
                    // the file is reached. 
                    while ((line = fileReader.ReadLine()) != null)
                    {
                        lineRawBytes = ParseLine(line);
                        if(lineRawBytes!=null)
                        {
                            blobList.AddRange(lineRawBytes);
                        }
                    }
                }
                mBlobValue = new byte[blobList.Count + sizeof(UInt32)];
                BitConverter.GetBytes((UInt32)blobList.Count).CopyTo(mBlobValue, 0);
                blobList.ToArray().CopyTo(mBlobValue, sizeof(UInt32));
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
        public TAS57xxCfgBlobValueIO(string fileName)
        {
            ImportFile(fileName);
        }
        public void ExportFile(string fileName)
        {
            File.WriteAllBytes(fileName, BlobValue);
        }
    }
}
